﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Couchbase;
using Couchbase.Core;

namespace SharpSoda.Storage
{
    public static class DataAccessHelper
    {
        private static Dictionary<string, IDbConnectionProvider> m_SqlClients = new Dictionary<string,IDbConnectionProvider>();

        public static void RegisterSqlClient(string name)
        {
            if(m_SqlClients.ContainsKey(name)) m_SqlClients.Remove(name);
            m_SqlClients.Add(name, new DbConnectionProvider());
        }

        public static IDbConnection GetSqlClient(string name)
        {
            IDbConnection result = null;
            if(m_SqlClients.ContainsKey(name)) result = m_SqlClients[name].OpenDbConnection();
            return result;
        }

        public static IBucket GetNoSqlDataSet(string name)
        {
            return ClusterHelper.GetBucket(name);
        }

        public static void LoadConfig(string nosqlconfig = "couchbase-clients/couchbase")
        {
            ClusterHelper.Initialize(nosqlconfig);

            foreach (var item in m_SqlClients)
            {
                item.Value.Initialize(item.Key);
            }
        }
    }
}
